<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>User</title>
    	<link rel="stylesheet" type="text/css" href="../themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="../themes/icon.css">
	<script type="text/javascript" src="../jquery.min.js"></script>
	<script type="text/javascript" src="../jquery.easyui.min.js"></script>

	
</head>
<body>
   <div class="easyui-layout" data-options="fit:true">
        <div data-options="region:'north'" style="height:50px">
        	   <a href="#" id="loadAllDataBtn" class="easyui-linkbutton" onclick="tab()"  data-options="iconCls:'icon-add'">切换分类</a>
        		<a href="#" id="addBtn" class="easyui-linkbutton" data-options="iconCls:'icon-add'">添加分类</a>
        		<a href="#" id="removesBtn" class="easyui-linkbutton" data-options="iconCls:'icon-remove'" onclick="deleteData()">删除分类</a>
        </div>
        <div data-options="region:'west',split:true" title="分类管理"  data-options="fit:true">
        	
        		<ul id="tt1" class="easyui-tree;" style="display: none;" data-options="url:'http://10.31.154.27:3000/cate/list/1',method:'post',animate:true"></ul>
        		<ul id="tt2" class="easyui-tree;" style="display: block;" data-options="url:'http://10.31.154.27:3000/cate/list/2',method:'post',animate:true"></ul>
        	
        </div>
     <!--   <div data-options="region:'center',title:'Main Title',iconCls:'icon-ok'">
            <table id="dg"></table>-->
            
            <div id="dlg" class="easyui-dialog" title="Basic Dialog" data-options="iconCls:'icon-save',closed:true" style="width:400px;height:400px;padding:10px">
		        <form id="ff" method="post">
		            <div style="margin-bottom:20px">
		            		<input class="easyui-textbox" name="_id">

		                <input class="easyui-textbox" name="text" style="width:100%" data-options="label:':',required:true">
		            </div>
		        </form>
		        <div style="text-align:center;padding:5px 0">
		            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()" style="width:80px">Submit</a>
		            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearForm()" style="width:80px">Clear</a>
		        </div>
		    </div>
       <!--</div>-->
    </div>
    <script>
    		var cateId = null;
    		var bstop=true;
    		   function tab(){
   		if(bstop){
    		$('#tt1').show();
    		$('#tt2').hide();
    		bstop=false;
    	}else{
    		$('#tt2').show();
    		$('#tt1').hide();
    		bstop=true;
    	}
   }
		
    		$('#tt1').tree({
		    url: "http://10.31.154.27:3000/cate/list/1",
		    method:'get',
		    onClick: function(node){
				cateId = node._id;
				cateType=node.type;
				$("#dg").datagrid({
					queryParams: {
						cateId: cateId
					}
				}).datagrid('reload');
			}
		});
		
		  	$('#tt2').tree({
		    url: "http://10.31.154.27:3000/cate/list/2",
		    method:'get',
		    onClick: function(node){
				cateId = node._id;
				cateType=node.type;
				$("#dg").datagrid({
					queryParams: {
						cateId: cateId
					}
				}).datagrid('reload');
			}
		});
    		$("#addBtn").click(function(){
    			$('#ff').form('clear');
    			if(cateId === null){
    				$.messager.show({
	                title:'信息提示',
	                msg:'请选择分类，否则无法添加产品',
	                timeout:3000,
	                showType:'show'
	            });
    			}else{
    				$('#dlg').dialog('open');
    			}
    		})
    		
    		//console.log(cateId);
    		function submitForm(){
			var postData = $("#ff").serializeArray();
    			//postData.cateId = cateId; 	
    			delete postData[0];
    			postData[3] = {};
    		    postData[3].name='parentId';
    		    postData[3].value=cateId;
    		    postData[4] = {};
    		    postData[4].name='type';
    		    postData[4].value=cateType;
    				$.ajax({
	    				type:"post",
	    				url:"http://10.31.154.27:3000/cate/data",
	    				data: postData,
	    				async:true
	    			}).done(function(res){
	    				// 这里要做一件事
	    				$.messager.show({
		                title:'信息提示',
		                msg:'数据添加成功',
		                timeout:3000,
		                showType:'show'
		           });
    			})	
    			$('#dlg').dialog('close');
    			$('#tt1').tree('reload');
    			$('#tt2').tree('reload');
        }
        function clearForm(){
            $('#ff').form('clear');
        }
    	
    	
    	
    	function deleteData(id){
    			$.messager.confirm('确认删除', '你确认删除数据吗？', function(r){
                if (r){
		    			$.ajax({
		    				type:"delete",
		    				url:"http://10.31.154.27:3000/cate/list/" + id,
		    				async:true
		    			}).done(function(res){
		    				$("#dg").datagrid('reload');
		    			})
                }
            });
    		}
    	
    	function deleteData(){
			var postData = $("#ff").serializeArray();
    			//postData.cateId = cateId; 	
    			delete postData[0];
    			postData[3] = {};
    		    postData[3].name='_id';
    		    postData[3].value=cateId;
    		    
    		    console.log(postData)
    				$.ajax({
	    				type:"delete",
	    				url:"http://10.31.154.27:3000/cate/list/"+postData[3].value,
	    				data: postData,
	    				async:true
	    			}).done(function(res){
	    				// 这里要做一件事
	    				$.messager.show({
		                title:'信息提示',
		                msg:'数据添加成功',
		                timeout:3000,
		                showType:'show'
		           });
    			})	
    			$('#tt1').tree('reload');
    			$('#tt2').tree('reload');
        }
        function clearForm(){
            $('#ff').form('clear');
        }
    	
    	
//  		function deleteData(id){
//  			var aaa = $("#ff").serializeArray();
//  			console.log(aaa)
//  			$.messager.confirm('确认删除', '你确认删除数据吗？', function(r){
//              if (r){
//		    			$.ajax({
//		    				type:"delete",
//		    				url:"http://10.31.154.27:3000/cate/list/" + id,
//		    				async:true
//		    			}).done(function(res){
//		    				$("#dg").datagrid('reload');
//		    			})
//              }
//          });
//  		}
    		
    		function editData(id){
    			$.ajax({
    				type:"put",
    				url:"http://10.31.154.27:3000/product/data/" +id,
    				async:true
    			}).done(function(res){
    				$('#ff').form('load', res);
    				$('#dlg').dialog('open');
    			})
    		}
    		
/*    		$("#removesBtn").click(function(){
    			var rows = $("#dg").datagrid('getSelections');
    			
    			if(rows.length>0){
    				var ids = [];
    				rows.forEach(function(item,idx){
    					ids.push(item._id);
    					$.ajax({
    					type:"delete",
    					url:"http://10.31.154.27:3000/product/data/"+ids[idx],
    					data:{
    						ids: ids.toString()
    					},
    					async:true
    				}).done(function(res){
    					$("#dg").datagrid('reload');
    				})
    				})
    			}
    			
    		})*/
    		
    		$("#loadAllDataBtn").click(function(){
    			cateId = null;
    			 $("#dg").datagrid({
				queryParams: {
					cateId: cateId
				}
			}).datagrid('reload');
    		})
    		
    		function doSearch(value){
            $("#dg").datagrid({
				queryParams: {
					cateId: cateId,
					title: value
				}
			}).datagrid('reload');
      }

    </script>
</body>
</html>